/*
	       File: Program.cs
	Create Date: 2008-08-11
	    Version:

	Description:


	 Change log:


*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;

using GuerillaProgrammer.Components.Logging;
using GuerillaProgrammer.FileProcessor;
using GuerillaProgrammer.FileProcessor.Settings;

namespace GuerillaProgrammer.FileProcessor.Console
{
    public class Program : PollingCallback 
    {
        #region Private member fields
        private Configuration _Configuration;
        private PollingController _PC;
        private ManualResetEvent _KeepGoing;
        private DirectoryProcessorController _DirectoryController;
        private FileProcessingController _FileController;
        int counter = 0;
        #endregion

        #region Constructors and cleanup methods
        public Program()
        {
            _Configuration = GetConfiguration();
            _FileController = new FileProcessingController(_Configuration);
            _DirectoryController = new DirectoryProcessorController(_Configuration, _FileController);
        }
        static void Main(string[] args)
        {
            Program theProgram = new Program();
            theProgram.Run();
        }
        #endregion

        #region Private implementation methods
        private Configuration GetConfiguration()
        {
            Logger.LogEnter();
            string configPath = System.Configuration.ConfigurationManager.AppSettings["ConfigFilePath"];
            if (false == File.Exists(configPath))
            {
                Logger.LogWarning("Configuration", "The config file is missing. We're done.");
                throw new FileNotFoundException("Configuration file is missing!");
            }
            Logger.LogLeave();
            return ConfigurationManager.ReadConfiguration(configPath);

        }
        private void FileProcessor()
        {
            _DirectoryController.ProcessDirectories();
        }
        #endregion

        #region Public interface
        public void Run()
        {
            System.Console.WriteLine("Starting processing loop");

            int pollingTime = 5;
            int.TryParse(_Configuration.Services.PollingTime, out pollingTime);

            _PC = new PollingController(pollingTime, (PollingCallback)this);
            _PC.StartPollingInterval();
            _KeepGoing = new ManualResetEvent(false);
            _KeepGoing.WaitOne(Timeout.Infinite, false);
        }
        #region PollingCallback Members
        public void Execute()
        {
            string message = string.Format("Timer - {0}", DateTime.Now.ToString("hh:mm:ss"));
            System.Console.WriteLine(message);
            FileProcessor();
            System.Console.WriteLine("Finished lap {0}", counter);
            counter++;
            //should add a check here to see if we should stop
            //bah!
            _PC.StartPollingInterval();
        }
        #endregion
        #endregion
    }
}
